package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Constant;
import com.example.demo.model.entity.UserModel;
import com.example.demo.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2018/4/22.
 */
@Controller
@RequestMapping(value = Constant.API_VERSION + "/enter")
public class EnterController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/CheckCode", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject checkCode(HttpServletRequest request) throws Exception {
        String uid = "";
        Map map = request.getParameterMap();
        Set keSet = map.entrySet();
        for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
            Map.Entry me = (Map.Entry) itr.next();
            Object ok = me.getKey();
            Object ov = me.getValue();
            String[] value = new String[1];
            if (ov instanceof String[]) {
                value = (String[]) ov;
            } else {
                value[0] = ov.toString();
            }

            for (int k = 0; k < value.length; k++) {
                uid = JSONObject.parseObject(value[k]).getString("CodeVal");
            }
        }
        JSONObject result = new JSONObject();
        UserModel userModel = userService.getByUid(uid);
        if (userModel.getEnterTime() == null) {
            if (userModel.getOutTime() != null) {
                userModel.setEnterTime(System.currentTimeMillis()); // TODO
                userModel.setOutTime(null);
            } else {
                userModel.setEnterTime(System.currentTimeMillis());
            }

            userService.save(userModel);
            result.put("Status", 1);
            result.put("StatusDesc", "验票成功");
        } else {
            result.put("Status", 0);
            result.put("StatusDesc", "已验票，请勿重复操作");
        }
        return result;
    }

    @RequestMapping(value = "/IsConnect", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject isConnect(HttpServletRequest request) throws Exception {
        JSONObject result = new JSONObject();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(new Date());
        result.put("DateTime", date);
        return result;
    }

//    @RequestMapping(value = "/out/CheckCode", method = RequestMethod.POST)
//    @ResponseBody
//    public JSONObject outCheckCode(HttpServletRequest request) throws Exception {
//        String uid = "";
//        Map map = request.getParameterMap();
//        Set keSet = map.entrySet();
//        for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
//            Map.Entry me = (Map.Entry) itr.next();
//            Object ok = me.getKey();
//            Object ov = me.getValue();
//            String[] value = new String[1];
//            if (ov instanceof String[]) {
//                value = (String[]) ov;
//            } else {
//                value[0] = ov.toString();
//            }
//
//            for (int k = 0; k < value.length; k++) {
//                uid = JSONObject.parseObject(value[k]).getString("CodeVal");
//            }
//        }
//        UserModel userModel = userService.getByUid(uid);
//
//
//        JSONObject result = new JSONObject();
//        result.put("Status", 1);
//        result.put("StatusDesc", "验票成功");
//        return result;
//    }
//
//    @RequestMapping(value = "/out/IsConnect", method = RequestMethod.POST)
//    @ResponseBody
//    public JSONObject outIsConnect(HttpServletRequest request) throws Exception {
//        JSONObject result = new JSONObject();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date = format.format(new Date());
//        result.put("DateTime", date);
//        return result;
//    }

}
